package day09_链表相关;

import java.util.Stack;

/**
 * https://leetcode.cn/problems/aMhZSa/submissions/
 */
public class Code02_剑指_Offer_回文链表_用栈解题 {

    public boolean isPalindrome(ListNode head) {
        if (head == null || head.next == null) {
            return true;
        }
        Stack<ListNode> tmpStack = new Stack<>();
        ListNode tmpHead = head;
        while (tmpHead != null) {
            tmpStack.push(tmpHead);
            tmpHead = tmpHead.next;
        }
        tmpHead = head;
        while (tmpHead != null) {
            if (tmpStack.pop().val != tmpHead.val) {
                return false;
            }
            tmpHead = tmpHead.next;
        }
        return true;
    }
}
